package vooga.xml.handlers;

import java.awt.Color;

import vooga.xml.XMLException;

/**
 * The XMLHandler for Vooga level definition files that parses Entities
 * based on FactoryHandler
 * @author Congyi Wu
 *
 */
public class EntityHandler extends vooga.xml.XMLHandler
{
    //XML constants
	public static final String LEVEL_ENTITY = "level";
    public static final String ENTITY_ENTITY = "entity";
    
    public static final String VALUE_ATTRIBUTE = "value";
    public static final String CLASS_ATTRIBUTE = "class";

    //whitespace delimited list of groups an Entity belongs to
    public static final String GROUPS_ATTRIBUTE = "groups";
    
    //zorder of Entity (integer)
    public static final String Z_ORDER_ATTRIBUTE = "zorder";
    
    //rgb color of entity (in hex, ie "ffffff" or "E2C232")
    public static final String COLOR_ATTRIBUTE = "color";
    
    //8 bit opacity of entity (in hex, from "00" to "ff" )
    public static final String OPACITY_ATTRIBUTE = "opacity";   

    public vooga.entities.Entity handleNode (org.w3c.dom.Node node, Object context) throws XMLException
    {
        try
        {
        	vooga.entities.Entity result = (vooga.entities.Entity)handleNodeHelper(node);
        	
        	String groups = getAttribute(node, GROUPS_ATTRIBUTE);
        	if(groups != null)
        		for(String g : groups.split("\\s"))
        			result.addGroup(g);

        	try
        	{
        		int zOrder = Integer.parseInt(getAttribute(node, Z_ORDER_ATTRIBUTE));
        		result.setZOrder(zOrder);
        	}
        	catch(NumberFormatException e) {}
        	
        	int color = result.getColor().getRGB();
        	int opacity = result.getColor().getAlpha();
        	
        	try
        	{
        		color = Integer.parseInt(getAttribute(node, COLOR_ATTRIBUTE), 16);
        	}
        	catch(NumberFormatException e) {}

        	try
        	{
        		opacity = Integer.parseInt(getAttribute(node, OPACITY_ATTRIBUTE), 16);
        		result.setColor(new Color(color));
        	}
        	catch(NumberFormatException e) {}
        	
        	result.setColor(new Color((opacity << 24) | (color & 0xFFFFFF), true));
        	
        	return result;
        }
        catch (Exception e)
        {
            throw new vooga.xml.XMLException("Could not handle " + node, e);
        }
    }
    
    private Object handleNodeHelper(org.w3c.dom.Node node) throws XMLException
    {
    	Object[] args = null;
        if (isComplete(node))
        {
            args = new Object[] { getAttribute(node, VALUE_ATTRIBUTE) };
        }
        else
        {
            java.util.List paramNodes = getSubNodes(node);
            args = new Object[paramNodes.size()];
            for (int k = 0; k < args.length; k++)
            {
                org.w3c.dom.Node current = (org.w3c.dom.Node)paramNodes.get(k);
                args[k] = getDocument().getHandler(current.getNodeName()).handleNode(current, null);
            }
        }
    	return vooga.util.Reflection.createInstance(getAttribute(node, CLASS_ATTRIBUTE), args);
    }

    protected boolean isComplete (org.w3c.dom.Node node)
    {
        return (getAttribute(node, VALUE_ATTRIBUTE) != null);
    }
}
